<?php


namespace App\common\models;


class ThreeLogin extends BaseModel
{

    //设置表名
    const TABLE_NAME = 'members_three_login';
    public $table = self::TABLE_NAME;
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $guarded = [];

    //添加记录
    public static function add($all) {
        $add = [
            'uid' => $all['uid'],
            'wx_unionid' => isset($all['wx_unionid']) ?$all['wx_unionid']: '',
            'wx_app_openid' => isset($all['wx_app_openid']) ?$all['wx_app_openid']: '',
            'wx_public_openid' => isset($all['wx_public_openid']) ?$all['wx_public_openid']: '',
            'wx_small_openid' => isset($all['wx_small_openid']) ?$all['wx_small_openid']: '',
            'apple_openid' => isset($all['apple_openid']) ?$all['apple_openid']: '',
            'qq_openid' => isset($all['qq_openid']) ?$all['qq_openid']: '',
            'wb_openid' => isset($all['wb_openid']) ?$all['wb_openid']: '',
            'type' => isset($all['type']) ?$all['type']: '',
            'create_at' => time(),
            'update_at' => time(),
        ];
        return self::insert($add);
    }

    //获取openid
    public static function getUserOpenid($uid, $field = '', $openid = '') {
        if (trim($openid)) return trim($openid);
        if (in_array($field, ['wx_app', 'wx_public', 'wx_small', 'wx_users_small', 'wx_store_small', 'apple', 'qq', 'wb'])) {
            return self::where('uid', $uid)->value($field . '_openid') ?: '';
        } elseif ($field == 'all') {
            $res = self::where('uid', $uid)->first();
            return $res ? $res->toArray() : [];
        } else {
            return '';
        }
    }

    //更新
    public static function updateThreeLogin($w, $up) {
        if (count($w) <= 0 || count($up) <= 0) return false;
        return self::where($w)->limit(1)->update($up);
    }

}
